$(function() {
    layui.form.verify({
        pwd: [/^\S{6,12}$/, '密码长度：6-16位'],
        diffPwd: function(newPwdStr) {
            const oldPwdStr = $('.layui-form [name=oldPwd]').val();
            if (oldPwdStr === newPwdStr) {
                return '新旧密码不能一致！'
            }
        },
        repwd(rePwdValue) {
            const pwdStr = $('.layui-form [name=newPwd]').val();

            if (pwdStr !== rePwdValue) {
                return '两次密码输入不一致！'
            }
        }
    });

    $('.layui-form').on('submit', doSubmit)

    function doSubmit(e) {
        //阻止表单默认行为
        e.preventDefault();
        //获取表单的数据
        let data = layui.form.val("userForm");
        data.reNewPwd = null;
        $.ajax({
            method: 'post',
            url: '/my/updatepwd',
            data,
            success(res) {
                if (res.status === 1) {
                    return layui.layer.alert(res.message)
                }
                layui.layer.alert('重置密码成功！', function() {
                    localStorage.removeItem('token')
                    window.parent.location.href = '/login.html'
                })
            }
        })
    }
});